package org.bluedream.core.utils.thymeleaf;

import com.alibaba.fastjson.JSONObject;
import org.bluedream.comm.utils.EmptyUtil;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring5.context.SpringContextUtils;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;


/**
 * 自定义标签逻辑类
 */
public class ThymeleafProcessor extends AbstractElementTagProcessor {
    //定义方言 标签
    private static final String TAG_NAME = "tml";
    private static final int PRECEDENCE = 10000;

    // 自定义属性 type：标签类型解析入口
    private static final String TAG_TYPE = "type";
    private static final String TAG_TYPE_TREE = "treeSelect";
    private static final String TAG_TYPE_SELECT = "select";

//    todo: 自定义标签 类型 测试变量
/*    private static final String TAG_URL = "url";                //请求地址
    private static final String TAG_TITLE = "title";            //标题
    private static final String TAG_ACTIVE_NAME = "name";            //表单绑定name
    private static final String TAG_ACTIVE_ID = "id";            //表单绑定id
    private static final String TAG_SUBMIT_NAME = "submitName";     //提交值
    private static final String TAG_SUBMIT = "submitValue";     //提交值
    private static final String TAG_SHOW = "showValue";         //回显内容

    private static final String TAG_OBJECT_TYPE = "objectType";       //组织 - 关系表，更新组织信息,type
    private static final String TAG_OBJECT_URL = "objectUrl";         //组织 - 关系表，更新组织信息,请求地址
    private static final String TAG_OBJECT_EL = "objectEl";           //组织 - 关系表，更新组织信息,更新目标元素ID前缀

    private static final String TAG_CAN_SELECT_ROOT = "canSelectRoot";      //根节点是否可选
    private static final String TAG_CAN_SELECT_PARENT = "canSelectParent";  //父节点是否可选
    private static final String TAG_EXCLUDE_CODE = "excludeCode";  //排除节点 元素name的值
    private static final String TAG_SHOW_CODE = "isShowCode";  //是否显示编码*/

    private boolean firstLoad = true;

    public ThymeleafProcessor(final String dialectPrefix) {
        super(
                TemplateMode.HTML,          // 此处理器将仅应用于HTML模式
                dialectPrefix,              // 要应用于名称的匹配前缀
                TAG_NAME,                   // 标签名称：匹配此名称的特定标签
                true,    // 将标签前缀应用于标签名称
                null,        // 无属性名称：将通过标签名称匹配
                false,  // 没有要应用于属性名称的前缀
                PRECEDENCE                 // 优先(内部方言自己的优先)
        );
    }

    /**
     * 核心处理器
     * @param context 上下文对象
     * @param tag     当前节点对象
     * @param handler
     */
    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler handler) {
        // 获取应用程序上下文。
        init(context);
        // 获取标签类型
        String tagTypeName = tag.getAttribute(TAG_TYPE).getValue();;
        IModel rootModel = null;
        // todo: TAG_TYPE_DICT常量用于测试：当treeSelect时，创建遮罩弹出的zTree页面
        if (EmptyUtil.isNoEmpty(tagTypeName)){
            ThymeleafTag thymeleafTag = new ThymeleafTag(context);
            String type = "";
            if (tagTypeName.equals(TAG_TYPE_TREE)){
                type = "zTree";
                rootModel = thymeleafTag.getModelTag(context , tag , type);
            }else if (tagTypeName.equals("orgTreeSelect")){
                rootModel = thymeleafTag.getLoginOrgTreeDataTag(context , tag , "zTree");
            }
            else {
                throw new RuntimeException("不支持的页面标签格式，当前标签为：" + tagTypeName + "，请确认" );
            }

        }
        handler.replaceWith(rootModel, false);
    }

    /**
     * 获取应用程序上下文。
     * @param context
     */
    private void init(ITemplateContext context){
        if (firstLoad){
            ApplicationContext appCtx = SpringContextUtils.getApplicationContext((ITemplateContext) context);
            firstLoad = false;
        }
    }
}
